<template>
  <div id="for-deposit">
    <!-- 缴纳保证金首页 -->
    <div class="deposit-content">
      <div class="top-bg">
        <van-nav-bar class="header" fixed :border="false">
          <div class="title" slot="title">{{title}}</div>
        </van-nav-bar>
      </div>
      <div class="reset-wrapper">
        <div class="reset-content bgwrite">
          <div class="reset-top">
            <div class="top-content" @click="changePayWay">
              <div class="no-account">
                <div class="tips-case">选择支付类型（支持微信、支付宝、银行卡）</div>
                <div class="right-case">
                  <img src="@/assets/img/performance/personalCenter/rightbluearrow.png" alt>
                </div>
              </div>
            </div>
          </div>
          <div class="reset-bottom">
            <div class="bottom-top">
              <div class="top-header">平台首次服务保证金</div>
              <div class="top-num">
                <span>￥</span>{{payCashMoney}}
              </div>
            </div>
            <div class="bottom-bottom">说明：平台首次服务保证金200，总服务保证金2000，剩余1800服务保证金将后续在订单里面按100元/单扣除。</div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="isPayWay" class="upDoor-popup pay-way-box">
      <div class="mask"></div>
      <div class="inner">
        <div class="flex flex-space-x handle-head">
          <div></div>
          <div class="title-name">请选择支付方式</div>
          <div class="close-block" v-on:click="isPayWay = false">
            <i class="iconfont icon-guanbi1"></i>
          </div>
        </div>
        <div class="pay-list">
          <div
            class="pay-item flex flex-center-y flex-space-x"
            v-for="(item, index) in payWayList"
            :key="index"
            v-on:click="switchPayWay(index)"
          >
            <div class="flex flex-center-y">
              <i
                v-if="item.icon"
                class="pay-way-wconfont iconfont"
                :class="[item.icon]"
                :style="item.styles"
              ></i>
              <img class="pay-way-wconfont" v-if="item.img" :src="item.img">
              <span>{{item.name}}</span>
            </div>
            <i v-if="payOrderPayWayActiveIndex === index" class="select-block iconfont icon-dui"></i>
            <i v-else></i>
          </div>
          <div class="pay-item flex flex-center-y flex-space-x" v-on:click="addBank">
            <div class="flex flex-center-y">
              <img class="pay-way-wconfont" src="@/assets/img/performance/homePage/bank.png">
              <span>银行卡账号</span>
            </div>
            <div class="add" v-on:click="addBank">
              <span>添加</span>
              <i class="iconfont icon-iconfonticonfonti2copycopy"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import { getuserUid, getUserLogin } from "@/utils/auth.js";
import { mapMutations, mapState } from "vuex";
import { Dialog } from "vant";
Vue.use(Dialog);
import { defaultPayWayList } from "@/pages/performance/homePage/children_page/pay_order/constant.js";
import { Copy } from "@/utils/object";
import { paying } from "@/utils/pay.js";
export default {
  components: {},
  data() {
    return {
      title: "缴纳服务保证金",
      accountShow: false,
      payWayList: defaultPayWayList,
      isPayWay: false,
      payOrderPayWayActiveIndex: 0,
      orderItem: "",
      orderUid: "",
      // 在支付中
      payRun: false,
      businessUserUid: getuserUid()
    };
  },
  computed: {
      ...mapState({
          payCashMoney: state => state.payCashMoney
      })
  },
  created() {
  },
  methods: {
    ...mapMutations(["UPDATE","setIsPay"]),
    diffBack() {
      this.$router.push("/performance/homePage");
    },
    // 取消选择
    closeDialog() {
      this.accountShow = false;
    },
    changePayWay() {
      // loading
      const toast = this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true, // 禁用背景点击
        mask: true,
        loadingType: "spinner",
        message: "加载中"
      });
      this.bankList();
    },
    addBank() {
      if (!this.businessUserUid) {
        this.$toast("加载中");
        return;
      }
      this.$router.push(
        "/performance/bindbank?type=3&userUid=" + this.businessUserUid
      );
    },
    async bankList() {
      let params = {
        userUid: getuserUid()
      };
      let bankResponse;
      try {
        // TEST toast
        await new Promise(e => {
          window.setTimeout(e, 1e3);
        });
        bankResponse = await HTTP("app/user/getUserBankCards", params);
        if (bankResponse.code !== 200) {
          this.$toast(bankResponse.msg);
          return;
        }
        console.log("银行卡列表");
        console.log(bankResponse.data);
        this.payWayList = [
          ...defaultPayWayList,
          ...bankResponse.data.map(e => {
            return {
              ...e,
              name: '银行卡 ' + `(${e.bankCardNumber.substr(
                e.bankCardNumber.length - 4
              )})`,
              img: require("@/assets/img/public/commonBank.png")
            };
          })
        ];
        // this.orightBankList = bankResponse.data
        this.isPayWay = true;
        this.$toast.clear();
      } catch (error) {
        this.$toast("数据开小差了");
      }
    },
    async switchPayWay(index) {
      if (this.payRun) {
        console.log("锁");
        return;
      }
      // this.UPDATE(['payOrderPayWayActiveIndex', index])
      this.payOrderPayWayActiveIndex = index;
      // 处理支付
      let payType =
        this.payOrderPayWayActiveIndex === 0
          ? 2
          : this.payOrderPayWayActiveIndex === 1
          ? 1
          : 3;

      let params = {
        userUid: getuserUid(),
        payType: payType
      };
      if (payType === 3) {
        params.bankCardUid = this.payWayList[
          this.payOrderPayWayActiveIndex
        ].uid;
      }
      this.payRun = true;
      // 请求支付信息
      let responseData = await HTTP("aliWechatBank", params);
      if(responseData.code !== 200){
        this.$toast(responseData.msg)
        return
      }
      // 处理请求
      let outTradeNo = responseData.outTradeNo;
      this.payRun = false;
      paying({
        url: 'aliWechatBank',
        params
      }, payType,responseData.data, outTradeNo, (outTradeNo) => {
        const toast = this.$toast.success({
          duration: 2000, // 持续展示 toast
          forbidClick: true, // 禁用背景点击
          message: "支付成功"
        });
        let second = 2;
        const timer = setInterval(() => {
          second--;
          if (!second) {
            clearInterval(timer);
            this.$toast.clear();
            this.$router.push('/performance/homePage')
          }
        }, 1000);
      }, ()=>0, ()=>{
          this.setIsPay(false)
          const toast = this.$toast.success({
            duration: 2000, // 持续展示 toast
            forbidClick: true, // 禁用背景点击
            message: "支付成功"
          });
          let second = 2;
          const timer = setInterval(() => {
            second--;
            if (!second) {
              clearInterval(timer);
              this.$toast.clear();
              this.$router.push('/performance/homePage')
            }
          }, 1000);
      });
    }
  }
};
</script>
<style lang="scss" scoped>
#for-deposit {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #f9f9f9;
  .deposit-content {
    position: relative;
    width: 100%;
    height: 100%;
    .top-bg {
      width: 100%;
      background: #fff;
      border-bottom-left-radius: 0.5rem;
      border-bottom-right-radius: 0.5rem;
      .header {
        background: #5e8efe;
        color: #ffffff;
        height: 0.92rem;
        .title {
          color: #ffffff;
          font-size: 0.35rem;
        }
      }
    }
    .reset-wrapper {
      position: fixed;
      width: 100%;
      padding: 0.24rem;
      box-sizing: border-box;
      top: 0.92rem;
      left: 0;
      right: 0;
      overflow-y: scroll;
      bottom: 1.6rem;
      .reset-content {
        width: 100%;
        border-radius: 0.1rem;
        .reset-top {
          .top-content {
            width: 100%;
            box-sizing: border-box;
            padding: 0.26rem 0.24rem;
            border-bottom: 0.01rem solid #e5e5e5;
            border-radius: 0.1rem 0.1rem 0 0;
            .has-account {
              width: 100%;
              display: flex;
              justify-content: space-around;
              align-items: center;
              text-align: center;
              .img-case {
                width: 0.64rem;
                height: 0.64rem;
                margin-right: 0.2rem;
                img {
                  width: 0.64rem;
                  height: 0.64rem;
                }
              }
              .account-case {
                width: 3rem;
                flex: 1;
                color: #282828;
                font-size: 0.28rem;
                font-family: PingFangSC-Regular;
                text-align: left;
              }
              .name-case {
                width: 3rem;
                text-align: right;
                font-size: 0.28rem;
                color: #959595;
                font-family: PingFangSC-Regular;
                font-weight: 400;
                margin-right: 0.2rem;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              }
              .right-case {
                width: 2.14rem;
                height: 2.14rem;
                img {
                  width: 0.14rem;
                  height: 0.26rem;
                }
              }
            }
            .no-account {
              width: 100%;
              display: flex;
              justify-content: space-around;
              align-items: center;
              text-align: center;
              .tips-case {
                flex: 1;
                width: 100%;
                color: #5e8efe;
                font-weight: 700;
              }
              .right-case {
                width: 0.5rem;
                height: 0.5rem;
                img {
                  width: 0.5rem;
                  height:0.5rem;
                }
              }
            }
          }
        }
        .reset-bottom {
          width: 100%;
          box-sizing: border-box;
          padding: 0 0.24rem;
          border-radius: 0 0 0.1rem 0.1rem;
          .bottom-top {
            padding: 0.5rem 0;
            border-bottom: 0.01rem solid #e5e5e5;
            .top-header {
              width: 100%;
              color: #656565;
              font-size: 0.28rem;
              font-family: PingFangSC-Regular;
              font-weight: 400;
              text-align: center;
            }
            .top-num {
              height: 100%;
              width: 100%;
              color: #5e8efe;
              font-family: PingFangSC-Semibold;
              font-weight: 600;
              font-size: 0.56rem;
              text-align: center;
              margin-top: 0.3rem;
              span {
                margin-right: 0.2rem;
              }
            }
          }
          .bottom-bottom {
            padding: 0.24rem 0;
            color: #656565;
            font-size: 0.24rem;
            font-family: PingFangSC-Regular;
            font-weight: 400;
            line-height: 0.4rem;
          }
        }
      }
    }
    .footer-btn {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
      padding: 0.36rem 0.24rem;
      box-sizing: border-box;
      height: 1.6rem;
      box-shadow: 0 0.02rem 0.04rem 0 rgba(216, 216, 216, 0.5);
      border-top: 0.01rem solid #d8d8d8;
      line-height: 1.6rem;
      .btn-pay {
        width: 100%;
        height: 0.88rem;
        background: #5e8efe;
        border-radius: 0.1rem;
        text-align: center;
        line-height: 0.88rem;
        color: #ffffff;
        font-family: PingFangSC-Semibold;
        font-weight: 600;
        font-size: 0.3rem;
      }
    }
  }
  .upDoor-popup {
    position: fixed;
    width: 100%;
    bottom: 0px;
    height: 100%;
    left: 0px;
    z-index: 100;
    .mask {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.5);
    }
    .inner {
      position: absolute;
      bottom: 0px;
      width: 100%;
      .handleColum {
        padding: 0.29rem 0.24rem;
        color: #656565;
        background: #fff;
        font-size: 0.32rem;
        .done {
          color: #5e8efe;
        }
      }
    }
  }
  .pay-way-box {
    .handle-head {
      border-radius: 0.3rem 0.3rem 0px 0px;
      background-color: #fff;
      height: 1rem;
      line-height: 1rem;
      width: 100%;
      .title-name {
        font-size: 0.32rem;
      }
      .close-block {
        margin-right: 0.24rem;
        .iconfont {
          color: #b1b1b1;
          font-size: 0.36rem;
        }
      }
    }
    .pay-list {
      font-size: 0.28rem;
      max-height: 75vh;
      overflow: auto;
      .pay-item {
        height: 1.36rem;
        border-top: 1px solid #ededed;
        background-color: #fff;
        padding: 0px 0.24rem;
        box-sizing: border-box;
        .pay-way-wconfont {
          width: 0.64rem;
          height: 0.64rem;
          font-size: 0.64rem;
          display: inline-block;
          margin-right: 0.2rem;
        }
        .select-block {
          display: inline-block;
          height: 0.24rem;
          font-size: 0.24rem !important;
          color: #5e8efe;
        }
        .add {
          color: $color-text-regular;
          font-size: 0.28rem;
          .iconfont {
            color: #aaa;
            font-size: 0.24rem;
          }
        }
      }
    }
  }
}
</style>
